package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.annotation.AutoFill;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.context.BaseContext;
import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.DishFlavor;
import com.sky.enumeration.OperationType;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.DishFlavorMapper;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SettmealDishMapper;
import com.sky.result.PageResult;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

@Service
public class DishServiceImpl implements DishService {

    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private DishFlavorMapper dishFlavorMapper;
    @Autowired
    private SettmealDishMapper settmealDishMapper;

    @Transactional
    @Override
    public void save(DishDTO dishDTO) {
        //向菜品插入一条数据
        Dish dish = new Dish();
        BeanUtils.copyProperties(dishDTO, dish);
        dishMapper.save(dish);
        long dishId=dish.getId();

        //向菜品口味表插入n条数据
        List<DishFlavor> flavors = dishDTO.getFlavors();
        //将每个值都关联到菜品的id

        if (flavors != null && flavors.size() > 0) {
            for (DishFlavor flavor : flavors) {
                flavor.setDishId(dishId);
            }
            dishFlavorMapper.insertBatch(flavors);
        }

    }

    /**
     * 菜品分页查询
     *
     * @param dishPageQueryDTO
     * @return
     */
    @Override
    public PageResult pageQuery(DishPageQueryDTO dishPageQueryDTO) {
        int page = dishPageQueryDTO.getPage();
        int pageSize = dishPageQueryDTO.getPageSize();
        PageHelper.startPage(page, pageSize);
        Page<DishVO> PageList =dishMapper.pageQuery(dishPageQueryDTO);
        return new PageResult(PageList.getTotal(), PageList.getResult());
    }

    /**
     * 批量删除菜品
     *
     * @param ids
     */
    @Transactional  //开启事务
    @Override
    public void delete(List<Long> ids) {
        //1.是否存在起售的菜品
        for (long id :ids) {
          Dish dish =  dishMapper.getById(id);
          if (dish.getStatus() == StatusConstant.ENABLE) {
              //起售的菜品不能删除
              throw new DeletionNotAllowedException(MessageConstant.DISH_ON_SALE);
          }
        }
        //2.是否与套餐进行关联
        List<Long> setmealIds = settmealDishMapper.selectCountByDishId(ids);
        if (setmealIds != null && setmealIds.size() > 0) {
            //有套餐进行关联，不可删除
            throw new DeletionNotAllowedException(MessageConstant.DISH_BE_RELATED_BY_SETMEAL);
        }
        //3.删除菜品，并删除相关口味
        dishMapper.deleteBatch(ids);
        dishFlavorMapper.deleteByDishIds(ids);

    }

    /**
     * 根据id  回显 菜品和对应的口味
     *
     * @param id
     * @return
     */
    @Override
    @Transactional
    public DishVO getById(Long id) {
        //根据Id查询菜品信息
        Dish dish = dishMapper.getById(id);
        //根据DishId查询菜品口味
        List<DishFlavor> dishFlavors = dishFlavorMapper.getByDishId(id);
        //将 相关信息 添加到VO中
        DishVO dishVO = new DishVO();
        BeanUtils.copyProperties(dish,dishVO);
        dishVO.setFlavors(dishFlavors);
        return dishVO;
    }

    /**
     * 修改菜品信息
     *
     * @param dishDTO
     */
    @Override
    @Transactional
    public void update(DishDTO dishDTO) {
        Dish dish = new Dish();
        BeanUtils.copyProperties(dishDTO,dish);
        List<DishFlavor> flavors=dishDTO.getFlavors();
        //对菜品信息继续修改
        dishMapper.update(dish);
        //修改当前菜品对应的口味数据
        dishFlavorMapper.deleteByDishId(dish.getId());
        if (flavors != null && flavors.size() > 0) {
            for (DishFlavor flavor : flavors) {
                flavor.setDishId(dish.getId());
            }
        }
        dishFlavorMapper.insertBatch(flavors);
    }


    /**
     * 启用禁用菜品
     *
     * @param status
     * @param id
     */
    @Override
    public void startOrStop(Integer status, long id) {
        Dish dish = Dish.builder()
                .id(id)
                .status(status)
                .build();
        dishMapper. update(dish);
    }

    /**
     * 根据分类id查询菜品 (起售状态）
     *
     * @param categoryId
     * @return
     */
    @Override
    public List<Dish> list(Long categoryId) {
        List<Dish> dishList = dishMapper.list(categoryId);
        return dishList;
    }

    /**
     * 根据分类id查询菜品和口味
     *
     * @param dish
     * @return
     */
    @Override
    public List<DishVO> listWithFlavor(Dish dish) {
        List<Dish>  dishList = dishMapper.list(dish.getCategoryId());
        List<DishVO> dishVOList = new ArrayList<>();
        for (int i = 0; i < dishList.size(); i++) {
            DishVO dishVO = new DishVO();
            BeanUtils.copyProperties(dishList.get(i),dishVO);
            List<DishFlavor> dishFlavors = dishFlavorMapper.getByDishId(dishList.get(i).getId());
            dishVO.setFlavors(dishFlavors);
            dishVOList.add(dishVO);
        }
        return dishVOList;
    }
}
